﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using Zatify.Domain.Abstract;

namespace Zatify.Domain.Concrete
{
    public class ZatifyEmailSender : IZatifyEmailSender
    {
        public void SendPasswordRecoveryToEmail(string name, string email, string companyName, string phone, string subject, string message)
        {
            var mail = new MailMessage();
            var SmtpServer = new SmtpClient("smtp.gmail.com");

            SmtpServer.EnableSsl = true;
            SmtpServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings.Get("SupportEmailAddress"), ConfigurationManager.AppSettings.Get("SupportEmailPassword"));
            SmtpServer.Port = 587; // Gmail works on this port 

            mail.IsBodyHtml = true;
            mail.From = new MailAddress(email);
            mail.To.Add(ConfigurationManager.AppSettings.Get("SupportEmailAddress"));
            mail.Subject = subject;
            mail.Body = BuildEmailMessage(name, email, companyName, phone, subject, message);

            SmtpServer.Send(mail);
        }

        public void SendContactUsToEmail(string email, string subject, string message)
        {
            var mail = new MailMessage();
            var SmtpServer = new SmtpClient("smtp.gmail.com");

            SmtpServer.EnableSsl = true;
            SmtpServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings.Get("SupportEmailAddress"), ConfigurationManager.AppSettings.Get("SupportEmailPassword"));
            SmtpServer.Port = 587; // Gmail works on this port 

            mail.IsBodyHtml = true;
            mail.From = new MailAddress(ConfigurationManager.AppSettings.Get("SupportEmailAddress"));
            mail.To.Add(email);
            mail.Subject = subject;
            mail.Body = string.Format("<b>Message:</b><br/><i>{0}</i>", message);
            
            SmtpServer.Send(mail);
        }

        #region Private Methods

        private string BuildEmailMessage(string name, string email, string companyName, string phone, string subject, string message)
        {
            return String.Format("<b>From:</b> <i>{0}</i><br/><b>Email:</b> <i>{1}</i><br/><b>Comapny Name:</b> <i>{2}</i><br/><b>Phone No:</b> <i>{3}</i><br/><br/><b>Subject:</b> <i>{4}</i><br/><br/><b>Message:</b> <i>{5}</i>",
                name, email, companyName, phone, subject, message);
        }

        #endregion
    }
}
